/*
In case it matters, this code is hereby placed under the MIT/X11 License:

Copyright (c) 2011, Kevin A. Wortman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef _DEB_KNAP_HH  // Guard against being included multiple times.
#define _DEB_KNAP_HH

#include <cassert>    // For assert(...).
#include <string>
#include <vector>

class Package {
public:
  Package(const std::string& name, int votes, int size)
    : _name(name),
      _votes(votes),
      _size(size) {
    assert(!name.empty());
    assert(votes >= 0);
    assert(size > 0);
  }

  // Getter functions.
  const std::string& name() const { return _name; }
  int votes() const { return _votes; }
  int size() const { return _size; }

private:
  std::string _name;
  int _votes;
  int _size;
};

// Alias for a vector of Package objects.
typedef std::vector<Package> PackageVector;

// Load a list of packages from a file formatted as described in the
// project handout.  Returns NULL on an I/O error.
PackageVector* loadPackages(const std::string& path);

// A set of package objects, implemented as an vector of indices into
// a PackageVector object, as returned from the loadPackages(...)
// function.  A PackageSet holds a reference to the master
// PackageVector object, so the PackageVector must continue to exist
// throughout the lifetime of the PackageSet.
class PackageSet {
public:
  PackageSet(const PackageVector& allPackages)
    : _allPackages(allPackages) { }

  // Copy constructor.
  PackageSet(const PackageSet& o)
    : _allPackages(o._allPackages),
      _indices(o._indices) { }

  // Getter function.
  const std::vector<int>& indices() const { return _indices; }

  // Return the sum of the votes (or size, resp.) fields of all the
  // packages in this set.
  int totalVotes() const;
  int totalSize() const;

  // Insert one index into the set.  The index is bounds-checked at
  // runtime using assert calls.
  void insert(int index);

  // Convert the set into a formatted string suitable for printing.
  // indent is a number of spaces to prepend to every line in the
  // output string.
  std::string show(int indent) const;

private:
  const PackageVector& _allPackages;
  std::vector<int> _indices;
};

#endif
